import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Login from "@/components/Login";
import Register from "@/components/Register";
import Home from "@/components/Home";
import MyCenter from "@/components/MyCenter";
import Course from "@/components/Course";
import Backstage from "@/components/Backstage";
import {Toast} from "vant";
const router = new VueRouter({
    mode: 'hash',
    // 配置路由
    routes: [
        {
            path: '/',
            redirect: '/course'
        },
        {
            path: '/test',
            component: ()=> import("@/components/Test")
        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/register',
            component: Register
        },
        {
            path: '/home',
            component: Home
        },
        {
            path: '/course',
            component: Course
        },
        {
            path: '/backstage',
            component: Backstage
        },
        {
            path: '/Center',
            component: MyCenter
        }
    ],
    linkActiveClass: 'test'
})
router.beforeEach((to, from, next)=> {
    if(localStorage.getItem('token') !== null || to.path === '/login' || to.path === '/Register' || to.path === '/course') {
        next();
    }else {
        Toast.fail('请登录');
        setTimeout(()=> {
            next('/login')
        }, 800)
    }
})
export default router;